/*
 * Helstius--Basic chemistry application
 * This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 * Copyright 2012 Jaked122
 */
package org.helstius.Gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import javax.swing.JTextField;
import java.awt.GridBagConstraints;
import javax.swing.JLabel;
import java.awt.Insets;
import javax.swing.JInternalFrame;
import javax.swing.JTabbedPane;
import javax.swing.JButton;

import org.helstius.chem.ChemicalEquation;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class EmpiricalFormulaGui extends JFrame {

	private JPanel contentPane;
	private JTextField FormulaField;
	private JLabel lblEmpirical;
	private JTextField formula2;
	private JTextField Molarweight;
	private JLabel lblMolarformula;
	private JLabel lblMolarWeight;

	/**
	 * Create the frame.
	 */
	public EmpiricalFormulaGui() {
		setTitle("Empirical Formula");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane, BorderLayout.CENTER);

		JPanel panel = new JPanel();
		tabbedPane.addTab("Empirical Formula Calc", null, panel, null);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 1.0, Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 1.0,
				Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		JLabel lblFormula = new JLabel("Formula");
		GridBagConstraints gbc_lblFormula = new GridBagConstraints();
		gbc_lblFormula.gridwidth = 7;
		gbc_lblFormula.insets = new Insets(0, 0, 5, 5);
		gbc_lblFormula.gridx = 0;
		gbc_lblFormula.gridy = 0;
		panel.add(lblFormula, gbc_lblFormula);

		FormulaField = new JTextField();
		lblFormula.setLabelFor(FormulaField);
		GridBagConstraints gbc_FormulaField = new GridBagConstraints();
		gbc_FormulaField.insets = new Insets(0, 0, 5, 0);
		gbc_FormulaField.fill = GridBagConstraints.BOTH;
		gbc_FormulaField.gridx = 7;
		gbc_FormulaField.gridy = 0;
		panel.add(FormulaField, gbc_FormulaField);
		FormulaField.setColumns(10);

		JButton CalculateButton = new JButton("Calculate");
		CalculateButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ChemicalEquation q = new ChemicalEquation(FormulaField
						.getText());
				lblEmpirical.setText(q.getEmpiricalEquation().toString());
			}
		});
		GridBagConstraints gbc_CalculateButton = new GridBagConstraints();
		gbc_CalculateButton.fill = GridBagConstraints.BOTH;
		gbc_CalculateButton.gridwidth = 8;
		gbc_CalculateButton.insets = new Insets(0, 0, 5, 0);
		gbc_CalculateButton.gridx = 0;
		gbc_CalculateButton.gridy = 1;
		panel.add(CalculateButton, gbc_CalculateButton);

		lblEmpirical = new JLabel("New label");
		GridBagConstraints gbc_lblEmpirical = new GridBagConstraints();
		gbc_lblEmpirical.gridwidth = 8;
		gbc_lblEmpirical.gridx = 0;
		gbc_lblEmpirical.gridy = 4;
		panel.add(lblEmpirical, gbc_lblEmpirical);

		JPanel panel_1 = new JPanel();
		tabbedPane.addTab("Molecular Formula calculation", null, panel_1, null);
		GridBagLayout gbl_panel_1 = new GridBagLayout();
		gbl_panel_1.columnWidths = new int[] { 0, 0, 0 };
		gbl_panel_1.rowHeights = new int[] { 0, 0, 0, 0, 0 };
		gbl_panel_1.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		gbl_panel_1.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE };
		panel_1.setLayout(gbl_panel_1);

		JLabel lblEmpiricalFormula = new JLabel("Empirical Formula");
		GridBagConstraints gbc_lblEmpiricalFormula = new GridBagConstraints();
		gbc_lblEmpiricalFormula.insets = new Insets(0, 0, 5, 5);
		gbc_lblEmpiricalFormula.anchor = GridBagConstraints.EAST;
		gbc_lblEmpiricalFormula.gridx = 0;
		gbc_lblEmpiricalFormula.gridy = 0;
		panel_1.add(lblEmpiricalFormula, gbc_lblEmpiricalFormula);

		formula2 = new JTextField();
		GridBagConstraints gbc_formula2 = new GridBagConstraints();
		gbc_formula2.insets = new Insets(0, 0, 5, 0);
		gbc_formula2.fill = GridBagConstraints.HORIZONTAL;
		gbc_formula2.gridx = 1;
		gbc_formula2.gridy = 0;
		panel_1.add(formula2, gbc_formula2);
		formula2.setColumns(10);

		lblMolarWeight = new JLabel("Molar Weight");
		GridBagConstraints gbc_lblMolarWeight = new GridBagConstraints();
		gbc_lblMolarWeight.anchor = GridBagConstraints.EAST;
		gbc_lblMolarWeight.insets = new Insets(0, 0, 5, 5);
		gbc_lblMolarWeight.gridx = 0;
		gbc_lblMolarWeight.gridy = 1;
		panel_1.add(lblMolarWeight, gbc_lblMolarWeight);

		Molarweight = new JTextField();
		GridBagConstraints gbc_Molarweight = new GridBagConstraints();
		gbc_Molarweight.insets = new Insets(0, 0, 5, 0);
		gbc_Molarweight.fill = GridBagConstraints.HORIZONTAL;
		gbc_Molarweight.gridx = 1;
		gbc_Molarweight.gridy = 1;
		panel_1.add(Molarweight, gbc_Molarweight);
		Molarweight.setColumns(10);

		JButton btnCalculate = new JButton("Calculate");
		btnCalculate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				ChemicalEquation a = new ChemicalEquation(formula2.getText());
				lblMolarWeight.setText(a.GetOriginal(a,
						Float.parseFloat(Molarweight.getText())).toString());
			}
		});
		GridBagConstraints gbc_btnCalculate = new GridBagConstraints();
		gbc_btnCalculate.insets = new Insets(0, 0, 5, 0);
		gbc_btnCalculate.gridx = 1;
		gbc_btnCalculate.gridy = 2;
		panel_1.add(btnCalculate, gbc_btnCalculate);

		lblMolarformula = new JLabel("MolarFormula");
		GridBagConstraints gbc_lblMolarformula = new GridBagConstraints();
		gbc_lblMolarformula.gridx = 1;
		gbc_lblMolarformula.gridy = 3;
		panel_1.add(lblMolarformula, gbc_lblMolarformula);
	}
}
